<?php
$auth = $_SESSION['auth'];
if($auth<90)
{
	echo '<center>Non hai l\'autorizzazione per accedere a questa pagina.<br><br><img src="images/accesso_negato.gif"></center>';
	exit();
}
//connessione al database
include 'connect.php';
$farmaci = mysql_query("select sum(quantita) from farmacia");
$farmaci2 = mysql_fetch_row($farmaci);
$numtotale = $farmaci2[0];
$farmaci3 = mysql_query("select sum(quantita) from farmacia where (DATEDIFF(farmacia.scadenza, current_date()) > 0) and (DATEDIFF(farmacia.scadenza, current_date()) < 32)");
$farmaci4 = mysql_fetch_row($farmaci3);
if($farmaci4[0] > 0)
	$numtotales = $farmaci4[0];
else
	$numtotales = 0;
$farmaci5 = mysql_query("select sum(quantita) from farmacia where DATEDIFF(farmacia.scadenza, current_date()) <= 0");
$farmaci6 = mysql_fetch_row($farmaci5);
if($farmaci6[0] > 0)
	$scaduti = $farmaci6[0];
else
	$scaduti = 0;
?>

<div id="wrapper">
    	<div id="secWrapper">
        	<div id="container" class="clearfix">
            	<div id="mainCol" class="clearfix">
				    
		<h3 id="why">Gestione Farmacia</h3><br>
                   
		<font size="2">
		
	Sono registrati <?php echo $numtotale; ?>  prodotti.<br><br>
		Dettaglio:<br><br>
		
		<table width="90%" border="1" cellpadding="5" cellspacing="0">
		<tr><th style='padding:5px' align='center'>Descrizione</th><th style='padding:5px' align='center'>Quantita'</th><th style='padding:5px' align='center'>Scadenza</th><th style='padding:5px' align='center'>Note</th></tr>
		
		<?php
		$farmaci = mysql_query("select * from farmacia order by descrizione");
		while($farmaci2 = mysql_fetch_array($farmaci))
		{?>
		<tr>
			<td style='padding:5px' align='center'><?php echo $farmaci2['descrizione']; ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['quantita']; ?></td>
			<td style='padding:5px' align='center'><?php  if($farmaci2['scadenza'] != "0000-00-00") echo date("d-m-Y", strtotime ($farmaci2['scadenza'])); ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['note']; ?></td>
		</tr>
		<?php
		}
		?>
		</table>
		
		<br>Questo mese scadono <?php echo $numtotales; ?> prodotti:<br>
		
		<?php if($numtotales > 0) { ?>
		<br>
		<table width="90%" border="1" cellpadding="5" cellspacing="0">
		<tr><th style='padding:5px' align='center'>Descrizione</th><th style='padding:5px' align='center'>Quantita'</th><th style='padding:5px' align='center'>Scadenza</th><th style='padding:5px' align='center'>Note</th></tr>
		
		<?php
		$farmaci = mysql_query("select * from farmacia where DATEDIFF(farmacia.scadenza, current_date()) > 0 and DATEDIFF(farmacia.scadenza, current_date()) < 32 order by descrizione");
		while($farmaci2 = mysql_fetch_array($farmaci))
		{?>
		<tr>
			<td style='padding:5px' align='center'><?php echo $farmaci2['descrizione']; ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['quantita']; ?></td>
			<td style='padding:5px' align='center'><?php echo date("d-m-Y", strtotime ($farmaci2['scadenza'])); ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['note']; ?></td>
		</tr>
		<?php
		}
		?>
		</table>
		<?php } ?>
		
				<br>Ci sono <?php echo $scaduti; ?> prodotti scaduti:<br>
				
		<?php if($scaduti > 0) { ?>
		<br>
		<table width="90%" border="1" cellpadding="5" cellspacing="0">
		<tr><th style='padding:5px' align='center'>Descrizione</th><th style='padding:5px' align='center'>Quantita'</th><th style='padding:5px' align='center'>Scadenza</th><th style='padding:5px' align='center'>Note</th><th></th></tr>
		
		<?php
		$farmaci = mysql_query("select * from farmacia where DATEDIFF(farmacia.scadenza, current_date()) <= 0 order by descrizione");
		while($farmaci2 = mysql_fetch_array($farmaci))
		{?>
		<tr>
			<td style='padding:5px' align='center'><?php echo $farmaci2['descrizione']; ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['quantita']; ?></td>
			<td style='padding:5px' align='center'><?php echo date("d-m-Y", strtotime ($farmaci2['scadenza'])); ?></td>
			<td style='padding:5px' align='center'><?php echo $farmaci2['note']; ?></td>
			<td style='padding:5px' align='center'><a href="javascript:validateForm(<?php echo$farmaci2['id']; ?>);">scarico</a></td>
		</tr>
		<?php
		}
		?>
		</table>
		<?php } ?>
		
	<br />
	<a href="home.php?page=magazzino">Torna al magazzino</a>
	
		</font>
                </div>
		
<script language="Javascript">

function validateForm(id) 
	{
		
		if (confirm("Operazione non reversibile, continuare?")) 
		{
			browser= navigator.appName;
			if (browser == "Netscape")
			window.location="scaricofarmaco2.php?id="+id; else window.location="scaricofarmaco2.php?id="+id;
		}	
	}
	
</script>
                